﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Model;
using Model.WWW_WWW.Answers;


namespace WebServices.WWW
{
    /// <summary>
    /// Answers 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://www.italki.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class Answers : System.Web.Services.WebService
    {
        /// <summary>
        /// 添加问题(T_Answers_Questions、T_Answers_Tag、T_NewsUpdates)(State:true执行成功,Effect:当前问题ID)
        /// </summary>       
        /// <returns>CommonMessage(State:true执行成功,Effect:当前问题ID)</returns>
        [WebMethod]
        public CommonMessage AddAskQuestion(string question, string desc, string ll, string bl, string tags, bool emailNotify, string status)
        {
            string memberno = HttpContext.Current.User.Identity.Name;            
            if (memberno == "")
            {
                return new CommonMessage();
            }
            QuestionModel qm = new QuestionModel();
            qm.MemberNo = memberno;
            qm.Question = HttpUtility.HtmlEncode(question);
            qm.Description = HttpUtility.HtmlEncode(desc);
            qm.LanguageLearn = ll;
            qm.BaseLanguage = bl;            
            qm.EmailNotify = emailNotify;
            qm.Status = status;
            return BLF.WWW_WWW.Answer.Answers.AddAskQuestion(qm, tags.Replace("?", "").Replace("/", "").Replace("-", "").Replace("&", ""));
        }
        /// <summary>
        /// 添加问题答案（回答问题）(T_Answers_Answers、T_NewsUpdates)
        /// </summary>       
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage AddQAnswer(int questionID, string answer)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            QAnswerModel am = new QAnswerModel();
            am.MemberNo = memberno;
            am.QuestionID = questionID;
            am.Answer = HttpUtility.HtmlEncode(answer);
            am.Points = 0;
            am.Status = "1";
            System.Collections.Generic.List<string> mailInfo;
            CommonMessage comMessage = BLF.WWW_WWW.Answer.Answers.AddQAnswer(am, out mailInfo);
            if (System.Text.RegularExpressions.Regex.IsMatch(mailInfo[0], "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
            {
                Lx.Web.Common.ControlsUI.BaseControl baseControl = new Lx.Web.Common.ControlsUI.BaseControl();
                string eSubject = Lx.Web.Common.UI.Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/AnswersNotify.xml", mailInfo[3])), mailInfo[3]), "email", "subject", "");
                string eBody = Lx.Web.Common.UI.Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/AnswersNotify.xml", mailInfo[3])), mailInfo[3]), "email", "body", "");
                eSubject = eSubject.Replace("{0}", mailInfo[1]);
                eBody = eBody.Replace("{0}", mailInfo[1]).Replace("{1}", mailInfo[2]).Replace("{2}", questionID.ToString()).Replace("{3}", DateTime.Now.ToString("HH:mm, dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo));
                new System.Threading.Thread(new System.Threading.ThreadStart(delegate
                {                  
                    Lx.Web.Common.UI.MsMailSend.SendMail(eSubject, eBody, mailInfo[0], null, true);
                })).Start();
            }
            return comMessage;
        }
        /// <summary>
        /// 答案打分
        /// </summary>
        /// <param name="answId">答案ID</param>
        /// <param name="mark">True 加分,false 减分</param>        
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage AnswerMark(int answId, bool mark)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Answer.Answers.AnswerMark(answId, mark, memberno);
        }
        /// <summary>
        /// 评选最佳答案
        /// </summary>
        /// <param name="status">‘2’最佳答案</param>
        /// <param name="qID">问题ID</param>
        /// <param name="aID">答案ID</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage AnswerVote(string status, int qID, int aID)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Answer.Answers.AnswerVote(status, qID, aID);
        }
    }
}

